package com.afteryuan.util.code;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * User: work25
 * Date: 2008-5-5
 * Time: 11:44:07
 */
public class GenerateServiceCode {
    private String servicePackageName;

    /**
     * 创建service接口文件
     *
     * @param targetRoot    要创建的文件所在文件夹路径
     * @param modelLocation 模型所在位置，import用的
     * @param fileName      文件名，带后缀不带路径
     */
    private void makeServiceFile(String targetRoot, String modelLocation, String fileName) {
        if (fileName.split("[.]").length > 1 && fileName.split("[.]")[1].equals("java")) {
            String suffix = fileName.split("[.]")[1];
            String modelName = fileName.split("[.]")[0];
            if (modelName.equals("BaseObject")) return;//BaseObject不创建service,impl,test
            String serviceName = modelName + "Service";
            File serviceFile = new File(targetRoot + "/" + serviceName + "." + suffix);
            if (!serviceFile.exists()) {
                try {
                    serviceFile.createNewFile();
                    FileWriter fw = new FileWriter(serviceFile);
                    String packageName = targetRoot.replaceAll("/", ".");
                    packageName = packageName.substring(packageName.indexOf("com.kmpro.service.cicada"));
                    fw.write("/**\n" +
                            " * Package:" + packageName + ";\n" +
                            " * $Id$\n" +
                            " * Copyright(c) 2001-2005 www.afteryuan.com\n" +
                            " */\n");

                    fw.write("package " + packageName + ";\n\n");
                    fw.write("import " + modelLocation + "." + modelName + ";\n");
                    if (!packageName.endsWith("kmpro.service")) {
                        fw.write("import com.kmpro.service.cicada.Service;\n");
                    }
                    fw.write("\n");
                    fw.write("/**\n" +
                            " * Service\n" +
                            " * <p/>\n" +
                            " * <p><a href=\"" + serviceName + ".java.html\"><i>View Source</i></a></p>\n" +
                            " *\n" +
                            " * @author <a href=\"mailto:afteryuan@gmail.com\">$" + "Author" + "$</a>\n" +
                            " * @version $" + "Revision" + "$\n" +
                            " */\n");
                    fw.write("public interface " + serviceName + " extends Service<" + modelName + "> {\n" +
                            "\n" +
                            "}");
                    fw.close();
                    System.out.println("file " + serviceFile.getName() + " is built to " + serviceFile.getPath());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 递归创建文件和文件夹等
     *
     * @param model   model所在文件夹路径
     * @param srcRoot 要创建的文件所在文件夹路径
     */
    public void buildFile(File model, File srcRoot) {
    String modelPackageName = getModelPackageName(model, srcRoot);
    String modelClassName = getModelClassName(model);
    String servicePackageName = getServicePackageName();
    }

    private String getModelClassName(File model) {
        return model.getName().substring(0,model.getName().indexOf(".java"));
    }

    private String getModelPackageName(File model, File srcRoot) {
        String modelPath = model.getPath();
        String srcPath = srcRoot.getPath();
        String modelPackagePath = modelPath.substring(srcPath.length(), modelPath.lastIndexOf(File.separator));
        String modelPackageName = modelPackagePath.replaceAll(getSeperar(), ".");
        return modelPackageName;
    }
    private String getSeperar(){
        if(File.separator.equals("\\")) return "\\\\";
        return File.separator;
    }

    public String getServicePackageName() {
        return servicePackageName.replaceAll("[.model]",".service");
    }
}